package com.healthtop.service.impl;

import com.google.common.collect.Maps;
import com.healthtop.common.Specification;
import com.healthtop.dao.XsMenuDao;
import com.healthtop.dto.MenuDto;
import com.healthtop.model.XsMenu;
import com.healthtop.model.XsRoleMenu;
import com.healthtop.service.DIYService;
import com.healthtop.service.XsMenuService;
import com.healthtop.service.XsRoleMenuService;
import com.healthtop.utils.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class XsMenuServiceImpl implements XsMenuService {

    @Autowired
    private XsMenuDao xsMenuDao;

    @Autowired
    private XsRoleMenuService xsRoleMenuService;

    @Autowired
    private DIYService diyService;

    @Override
    public MenuDto selectMenuById(MenuDto menuDto) {
        if(Specification.OperateMode.UPDATE.index==menuDto.getMode()){
            XsMenu xsMenu = xsMenuDao.selectById(menuDto.getMenuId());
            BeanUtils.copyProperties(xsMenu,menuDto);
        }
        return menuDto;
    }

    @Override
    public void operateMenu(MenuDto menuDto) {
        XsMenu xsMenu = new XsMenu();
        BeanUtils.copyProperties(menuDto,xsMenu);
        if(Specification.OperateMode.CREATE.index==menuDto.getMode()){
            xsMenu.setDelFalg("0");
            xsMenu.setCreateTime(new Date());
            xsMenu.setCreateUser(UserUtils.getCurrentUser().getUid());
            xsMenuDao.insert(xsMenu);
        }else{
            xsMenu.setUpdateTime(new Date());
            xsMenu.setUpdateUser(UserUtils.getCurrentUser().getUid());
            xsMenuDao.update(xsMenu);
        }
    }

    @Override
    public void delete(Long id) {
       XsMenu xsMenu = xsMenuDao.selectById(id);
       if(!ObjectUtils.isEmpty(xsMenu)){
           xsMenu.setDelFalg("1");
           xsMenu.setDelTime(new Date());
           xsMenuDao.update(xsMenu);
       }
    }

    @Override
    public List<XsMenu> selectParentMenu() {
        return xsMenuDao.selectParentMenu();
    }

    @Override
    public List<Map> getMenusByRoleId(long roleId) {
        List<Long> rrIds = diyService.selectMenuIdByRoleId(roleId);
        List<Map> menus = new ArrayList<>();
        List<XsMenu> list = xsMenuDao.selectParentMenu();
        for(XsMenu parent : list){
          Map map  = Maps.newHashMap();
          map.put("id",parent.getMenuId());
          map.put("name",parent.getMenuName());
            if(rrIds.contains(parent.getMenuId())) {
                map.put("checked",true);
            }
            map.put("open",true);
            menus.add(map);

            List<XsMenu> subList = xsMenuDao.selectMenuByPid(parent.getMenuId());
            List<Map> children = new ArrayList<>();
            for(XsMenu sub : subList){
                Map map1  = Maps.newHashMap();
                map1.put("id",sub.getMenuId());
                map1.put("name",sub.getMenuName());
                if(rrIds.contains(sub.getMenuId())) {
                    map1.put("checked",true);
                }
                map1.put("isParent",false);
                children.add(map1);
            }
            map.put("children",children);
        }
        return menus;
    }

    @Override
    public XsMenu getMenuByPath(String servletPath) {
        return xsMenuDao.getMenuByPath(servletPath);
    }
}
